-- SPDX-License-Identifier: GPL-3.0-or-later

local extension = Package:new("rfenghou_hegAdvantage", Package.CardPack)
extension.extensionName = "aaa_fenghou"

Fk:loadTranslationTable{
  ["rfenghou_hegAdvantage"] = "封侯国战卡牌",
}

local lureTigerSkill = fk.CreateActiveSkill{
  name = "lure_tiger_skill",
  prompt = "#lure_tiger_skill",
  can_use = Util.CanUse,
  min_target_num = 1,
  max_target_num = 2,
  mod_target_filter = function(self, to_select, selected, player)
    return to_select ~= player.id
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, effect)
    local target = room:getPlayerById(effect.to)
    room:setPlayerMark(target, "@@lure_tiger-turn", 1)
    room:setPlayerMark(target, MarkEnum.PlayerRemoved .. "-turn", 1)
    room:handleAddLoseSkills(target, "#lure_tiger_hp|#lure_tiger_prohibit", nil, false, true) -- global...
    room.logic:trigger("fk.RemoveStateChanged", target, nil) -- FIXME
  end,
}
local lureTigerProhibit = fk.CreateProhibitSkill{
  name = "#lure_tiger_prohibit",
  -- global = true,
  prohibit_use = function(self, player, card)
    return player:getMark("@@lure_tiger-turn") ~= 0
  end,
  is_prohibited = function(self, from, to, card)
    return to:getMark("@@lure_tiger-turn") ~= 0
  end,
}
local lureTigerHp = fk.CreateTriggerSkill{
  name = "#lure_tiger_hp",
  -- global = true,
  refresh_events = {fk.PreHpRecover, fk.PreHpLost, fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    return target == player and player:getMark("@@lure_tiger-turn") ~= 0
  end,
  on_refresh = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
      data.damage = 0
    else
      data.num = 0
    end
    return true
  end,
}
Fk:addSkill(lureTigerProhibit)
Fk:addSkill(lureTigerHp)
local lureTiger = fk.CreateTrickCard{
  name = "&lure_tiger",
  skill = lureTigerSkill,
  suit = Card.Heart,
  number = 2,
  multiple_targets = true,
}
extension:addCard(lureTiger)

Fk:loadTranslationTable{
  ["lure_tiger"] = "调虎离山",
  [":lure_tiger"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一至两名其他角色<br/><b>效果</b>：目标角色于此回合内不计入距离和座次的计算，且不能使用牌，且不是牌的合法目标，且体力值不会改变。",
  ["#lure_tiger_prohibit"] = "调虎离山",
  ["#lure_tiger_skill"] = "选择一至两名其他角色，这些角色于此回合内不计入距离和座次的计算，<br/>且不能使用牌，且不是牌的合法目标，且体力值不会改变",

  ["@@lure_tiger-turn"] = "调虎离山",
}

local befriendAttackingSkill = fk.CreateActiveSkill{
  name = "befriend_attacking_skill",
  prompt = "#befriend_attacking_skill",
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, player)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return player ~= target and player.kingdom ~= target.kingdom
  end,
  target_filter = Util.TargetFilter,
  can_use = function(self, player, card)
    return not player:prohibitUse(card)
  end,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.to)
    if target.dead then return end
    target:drawCards(1, "befriend_attacking")
    if player.dead then return end
    player:drawCards(3, "befriend_attacking")
  end
}
local befriendAttacking = fk.CreateTrickCard{
  name = "&befriend_attacking",
  skill = befriendAttackingSkill,
}
extension:addCards({
  befriendAttacking:clone(Card.Heart, 9),
})
Fk:loadTranslationTable{
  ["befriend_attacking"] = "远交近攻",
  ["befriend_attacking_skill"] = "远交近攻",
  [":befriend_attacking"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：势力与你不同的一名角色<br/><b>效果</b>：目标角色"..
  "摸一张牌，然后你摸三张牌。（身份化仅对比势力不同，无势力可对有势力角色使用）",
  ["#befriend_attacking_skill"] = "选择势力与你不同的一名角色，其摸一张牌，你摸三张牌",
}

local knownBothSkill = fk.CreateActiveSkill{
  name = "known_both_skill",
  prompt = "#known_both_skill",
  can_use = Util.CanUse,
  target_num = 1,
  mod_target_filter = function(self, to_select, selected, player)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return player ~= target and (not target:isKongcheng() or target.general == "anjiang" or target.deputyGeneral == "anjiang")
  end,
  target_filter = Util.TargetFilter,
  on_effect = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.to)
    if target.dead or player.dead then return end
    local all_choices = {"known_both_main", "known_both_deputy", "known_both_hand"}
    local choices = table.clone(all_choices)
    if target:isKongcheng() then
      table.remove(choices)
    end
    if target.general ~= "anjiang" then
      table.remove(choices, 1)
    end
    if target.deputyGeneral ~= "anjiang" then
      table.removeOne(choices, "known_both_deputy")
    end
    if #choices == 0 then return end
    local choice = room:askForChoice(player, choices, self.name, "#known_both-choice::"..target.id, false, all_choices)
    if choice == "known_both_hand" then
      U.viewCards(player, target:getCardIds(Player.Hand), self.name, "#known_both-hand::"..target.id)
      room:sendLog{
        type = "#know_hand",
        from = player.id,
        toast = true,
      }
    else
      local general = choice == "known_both_main" and {target:getMark("__heg_general"), target.deputyGeneral, tostring(target.seat)} or {target.general, target:getMark("__heg_deputy"), tostring(target.seat)}
      room:askForCustomDialog(player, self.name, "packages/aaa_fenghou/qml/KnownBothBox.qml", general)
      room:sendLog{
        type = "#know_general",
        from = player.id,
        toast = true,
      }
    end
  end,
}
local knownBoth = fk.CreateTrickCard{
  name = "&known_both",
  skill = knownBothSkill,
  special_skills = {"recast"},
}
extension:addCards{
  knownBoth:clone(Card.Club, 3),
}
Fk:loadTranslationTable{
  ["known_both"] = "知己知彼",
  ["known_both_skill"] = "知己知彼",
  [":known_both"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：一名其他角色<br/><b>效果</b>：观看目标角色一张暗置的武将牌或其手牌。",
  ["#known_both-choice"] = "知己知彼：选择对 %dest 执行的一项",
  ["known_both_main"] = "观看主将",
  ["known_both_deputy"] = "观看副将",
  ["known_both_hand"] = "观看手牌",
  ["#KnownBothGeneral"] = "观看 %1 武将",
  ["#known_both-hand"] = "知己知彼：观看%dest的手牌",
  ["#known_both_skill"] = "选择一名其他角色，观看其一张暗置的武将牌或其手牌",
  ["#know_hand"] = "%from 观看了手牌",
  ["#know_general"] = "%from 观看了武将",
}

local awaitExhaustedSkill = fk.CreateActiveSkill{
  name = "await_exhausted_skill",
  prompt = "#await_exhausted_skill",
  mod_target_filter = function(self, to_select, selected, player)
    local target = Fk:currentRoom():getPlayerById(to_select)
    return target.kingdom == player.kingdom
  end,
  can_use = function(self, player, card)
    if player:prohibitUse(card) then return end
    for _, p in ipairs(Fk:currentRoom().alive_players) do
      if not player:isProhibited(p, card) and self:modTargetFilter(p.id, {}, player, card, true) then
        return true
      end
    end
  end,
  on_use = function(self, room, use)
    if not use.tos or #TargetGroup:getRealTargets(use.tos) == 0 then
      local player = room:getPlayerById(use.from)
      if player.kingdom == "unknown" then
        use.tos = {{use.from}}
      else
        use.tos = {}
        for _, p in ipairs(room:getAlivePlayers()) do
          if not player:isProhibited(p, use.card) and p.kingdom == player.kingdom then --权宜
            TargetGroup:pushTargets(use.tos, p.id)
          end
        end
      end
    end
  end,
  on_effect = function(self, room, effect)
    local target = room:getPlayerById(effect.to)
    if target.dead then return end
    target:drawCards(2, "await_exhausted")
    if target.dead then return end
      room:askForDiscard(target, 2, 2, true, self.name, false)
  end,
}
local awaitExhausted = fk.CreateTrickCard{
  name = "&await_exhausted",
  skill = awaitExhaustedSkill,
  multiple_targets = true,
}
extension:addCards({
  awaitExhausted:clone(Card.Heart, 11),
})
Fk:loadTranslationTable{
  ["await_exhausted"] = "以逸待劳",
  ["await_exhausted_skill"] = "以逸待劳",
  [":await_exhausted"] = "锦囊牌<br/><b>时机</b>：出牌阶段<br/><b>目标</b>：你和与你势力相同的角色<br/><b>效果</b>：每名目标角色各摸两张牌，"..
  "然后弃置两张牌。",
  ["#await_exhausted_skill"] = "你和与你势力相同的角色各摸两张牌，然后弃置两张牌",
}

local dragonPhoenixSkill = fk.CreateTriggerSkill{
  name = "#dragon_phoenix_skill",
  attached_equip = "dragon_phoenix",
  events = {fk.TargetSpecified, fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.TargetSpecified then
      if target == player and data.card and data.card.trueName == "slash" then
        return not player.room:getPlayerById(data.to):isNude()
      end
    else
      return data.damage and data.damage.from == player and not target:isKongcheng() and player.room.logic:damageByCardEffect() and data.damage.card.trueName == "slash"
    end
  end,
  on_cost = function(self, event, target, player, data)
    local prompt = event == fk.TargetSpecified and "#dragon_phoenix-slash::" .. data.to or "#dragon_phoenix-dying::" .. target.id
    return player.room:askForSkillInvoke(player, self.name, data, prompt)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, "dragon_phoenix", "control")
    if event == fk.TargetSpecified then
      local to = player.room:getPlayerById(data.to)
      room:askForDiscard(to, 1, 1, true, self.name, false, ".", "#dragon_phoenix-invoke")
    else
      local card = room:askForCardChosen(player, target, "h", self.name)
      room:obtainCard(player, card, false, fk.ReasonPrey)
    end
  end,
}
Fk:addSkill(dragonPhoenixSkill)

local dragonPhoenix = fk.CreateWeapon{
  name = "&dragon_phoenix",
  suit = Card.Spade,
  number = 2,
  attack_range = 2,
  equip_skill = dragonPhoenixSkill,
}
extension:addCard(dragonPhoenix)

Fk:loadTranslationTable{
  ["dragon_phoenix"] = "飞龙夺凤",
  [":dragon_phoenix"] = "装备牌·武器<br/><b>攻击范围</b>：２ <br/><b>武器技能</b>：①当你使用【杀】指定目标后，你可令目标弃置一张牌。②当一名角色因执行你使用的【杀】的效果而受到你造成的伤害而进入濒死状态后，你可获得其一张手牌。",
  ["#dragon_phoenix_skill"] = "飞龙夺凤",
  ["#dragon_phoenix-slash"] = "飞龙夺凤：你可令 %dest 弃置一张牌",
  ["#dragon_phoenix-dying"] = "飞龙夺凤：你可获得 %dest 一张手牌",
  ["#dragon_phoenix-invoke"] = "受到“飞龙夺凤”影响，你需弃置一张牌",
}

return extension
